{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7150ede3-a0d7-4341-aa51-52e15d13c9f9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using CPython \u001b[36m3.12.12\u001b[39m\n",
      "Creating virtual environment at: \u001b[36m.venv\u001b[39m\n",
      "Activate with: \u001b[32msource .venv/bin/activate\u001b[39m\n"
     ]
    }
   ],
   "source": [
    "!uv venv .venv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e35dfaaf-2900-43cd-b34f-8a5ca52efd1b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!source .venv/bin/activate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "46f0c99e-84fd-42e8-b031-ea8b79e3f7f7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[2K\u001b[2mResolved \u001b[1m2 packages\u001b[0m \u001b[2min 664ms\u001b[0m\u001b[0m                                         \u001b[0m\n",
      "\u001b[2K\u001b[2mPrepared \u001b[1m2 packages\u001b[0m \u001b[2min 751ms\u001b[0m\u001b[0m                                             \n",
      "\u001b[2K\u001b[2mInstalled \u001b[1m2 packages\u001b[0m \u001b[2min 37ms\u001b[0m\u001b[0m                                \u001b[0m\n",
      " \u001b[32m+\u001b[39m \u001b[1mpyserial\u001b[0m\u001b[2m==3.5\u001b[0m\n",
      " \u001b[32m+\u001b[39m \u001b[1msmartpi\u001b[0m\u001b[2m==0.1.22\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!uv pip install pyserial smartpi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92f71a65-89ad-4e7d-86ea-67c1aa046f25",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[2K\u001b[2mResolved \u001b[1m30 packages\u001b[0m \u001b[2min 14.76s\u001b[0m\u001b[0m                                       \u001b[0m\n",
      "\u001b[2K\u001b[37m⠋\u001b[0m \u001b[2mPreparing packages...\u001b[0m (29/30)                                                 "
     ]
    }
   ],
   "source": [
    "# 1. 用 uv 直接装 pip + ipykernel\n",
    "!uv pip install --python .venv/bin/python pip ipykernel\n",
    "\n",
    "# 2. 注册内核\n",
    "! .venv/bin/python -m ipykernel install --user --name=uv-env --display-name \"Python (lingxin-uv-env)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "558f0d36-89a8-46bd-8f79-a7bae3227356",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "18d76bfb-9352-484e-9b88-27ccc400aa83",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from smartpi import base_driver\n",
    "\n",
    "def read_all_info():\n",
    "    \"\"\"读取并格式化显示灵芯派基础信息\"\"\"\n",
    "    device_model = base_driver.read_device_model()\n",
    "    version      = base_driver.read_version()\n",
    "    factory_data = base_driver.read_factory_data()\n",
    "    hardware_id  = base_driver.read_hardware_ID()\n",
    "    device_name  = base_driver.read_device_name()\n",
    "    connected    = base_driver.read_connected()\n",
    "    battery      = base_driver.read_battery()\n",
    "\n",
    "    print(\"========== 灵芯派设备信息 ==========\")\n",
    "    print(f\"设备型号 : {device_model}\")\n",
    "    print(f\"固件版本 : {version}\")\n",
    "    print(f\"厂家信息 : {factory_data}\")\n",
    "    print(f\"硬件 ID  : {hardware_id}\")\n",
    "    print(f\"设备名称 : {device_name}\")\n",
    "    print(f\"连接方式 : {connected}\")\n",
    "    print(f\"电池电量 : {battery}\")\n",
    "    print(\"===================================\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a7aa2fcd-b3fc-4a48-9af6-379344e101de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "========== 灵芯派设备信息 ==========\n",
      "设备型号 : RK-CON\n",
      "固件版本 : V1.0.0\n",
      "厂家信息 : ZMROBO\n",
      "硬件 ID  : 123456789ABC\n",
      "设备名称 : Martin\n",
      "连接方式 : USB\n",
      "电池电量 : 98\n",
      "===================================\n"
     ]
    }
   ],
   "source": [
    "read_all_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ff57f1ea-d629-4758-9970-61e0a4b4c1f9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测距数据(int) : 0\n"
     ]
    }
   ],
   "source": [
    "from smartpi import ultrasonic\n",
    "distance_data=ultrasonic.get_value(1)\n",
    "print(f\"测距数据(int) : {distance_data}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "99e4e328-595c-44a2-946b-7b4f925df09b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "触碰数据(bytes) : 0\n"
     ]
    }
   ],
   "source": [
    "from smartpi import touch_sensor\n",
    "touch_data=touch_sensor.get_value(1)\n",
    "print(f\"触碰数据(bytes) : {touch_data}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e48fc6e1-b4ed-489f-aceb-5328873d6d97",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编码值(int) : 3003\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from smartpi import motor\n",
    "motor_code=motor.get_motor_encoder(1)\n",
    "print(f\"编码值(int) : {motor_code}\")\n",
    "motor.set_motor_angle(1,44,500) # left run\n",
    "motor.set_motor_angle(2,44,500) # right run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2e2ef920-0998-4e2e-94d3-1c7ffbee2cba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测距数据(int) : 13\n"
     ]
    }
   ],
   "source": [
    "from smartpi import ultrasonic\n",
    "distance_data=ultrasonic.get_value(6)\n",
    "print(f\"测距数据(int) : {distance_data}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "956691f7-b271-4707-bdbc-0ccdf592cd83",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mKeyboardInterrupt\u001b[39m                         Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[31]\u001b[39m\u001b[32m, line 7\u001b[39m\n\u001b[32m      5\u001b[39m i = \u001b[32m0\u001b[39m\n\u001b[32m      6\u001b[39m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m     \u001b[43mtime\u001b[49m\u001b[43m.\u001b[49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[32;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m      8\u001b[39m     led.set_color(\u001b[32m5\u001b[39m,i)\n\u001b[32m      9\u001b[39m     \u001b[38;5;28;01mif\u001b[39;00m i<\u001b[32m8\u001b[39m:\n",
      "\u001b[31mKeyboardInterrupt\u001b[39m: "
     ]
    }
   ],
   "source": [
    "# 设置彩灯颜色：port:连接P端口；\n",
    "# command:0:关灯；1:红；2:绿；3:蓝；4:黄；5:紫；6:青；7:白；\n",
    "from smartpi import led\n",
    "import time\n",
    "i = 0\n",
    "while True:\n",
    "    time.sleep(1)\n",
    "    led.set_color(5,i)\n",
    "    if i<8:\n",
    "        i = i+1\n",
    "    else:\n",
    "        i=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2220824a-c33e-4489-85f3-a6fad2431e83",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd86a50e-2632-49bc-b07f-75faf6e89390",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (lingxin-uv-env)",
   "language": "python",
   "name": "uv-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
